import { Request, Response } from "express";
import articleServer from "@src/services/articleServer";
import article from "@src/models/article";
import articleSql from "@src/sql/articleSql";

const paths = {
  list: "/article-list/:id",
  detail: "/article-detail/:id",
  recommend: "/article-recommend",
};

// 列表
const list = async (req: Request, res: Response) => {
  const page = req.query.page ? req.query.page : 1;
  const size = req.query.size ? req.query.size : 15;
  const filters = req.query.filters ? req.query.filters : {};
  const { id } = req.params;
  const data: any = await articleServer.frontList(Number(page), Number(size), id, filters);
  return res.status(data.code).json({ data });
};

// 详情
const detail = async (req: Request, res: Response) => {
  const { id } = req.params;
  const data: any = await articleServer.detail(id);
  await article.increment({ visit_num: 1 }, { where: { id } });
  return res.status(data.code).json({
    data,
  });
};

// 推荐的文章
const recommend = async (req: Request, res: Response) => {
  const data = await articleSql.recommend();
  return res.status(data.code).json({
    data,
  });
};

export default {
  paths,
  list,
  detail,
  recommend,
};